import 'package:flutter/material.dart';
import 'package:hook_up_rent/widgets/common_image.dart';

class SearchBar extends StatefulWidget {
  final bool? showLocation; //是否显示位置
  final Function? goBackCallback; //回退
  final String? inputValue; //搜索框值
  final String defaultInputValue; //默认显示值
  final Function? onCancel; //取消按钮
  final bool? showMap; //是否显示地图按钮
  final Function? onSearch; //点击搜索框触发
  final ValueChanged<String>? onSearchSubmit; // 点击回车触发
  const SearchBar(
      {super.key,
      this.showLocation,
      this.goBackCallback,
      this.inputValue = '',
      this.defaultInputValue = '请输入搜索词',
      this.onCancel,
      this.showMap,
      this.onSearch,
      this.onSearchSubmit});

  @override
  State<SearchBar> createState() => _SearchBarState();
}

class _SearchBarState extends State<SearchBar> {
  String _searchWord = '';
  late TextEditingController _controller;
  late FocusNode _focus; // 焦点对象
  void _onClean() {
    setState(() {
      _controller.clear();
      _searchWord = '';
    });
  }

  @override
  void initState() {
    super.initState();
    _focus = FocusNode();
    _controller = TextEditingController(text: widget.inputValue);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Row(children: [
        if (widget.showLocation ?? false) // 默认为 false
          Padding(
            padding: const EdgeInsets.only(right: 10),
            child: GestureDetector(
              onTap: () {},
              child: Row(
                children: const [
                  Icon(
                    Icons.room,
                    color: Colors.green,
                    size: 14,
                  ),
                  Padding(padding: EdgeInsets.only(left: 2)),
                  Text(
                    '北京',
                    style: TextStyle(color: Colors.black, fontSize: 14),
                  ),
                ],
              ),
            ),
          ),
        // 返回按钮
        if (widget.goBackCallback != null || false)
          Padding(
            padding: const EdgeInsets.only(right: 10),
            child: GestureDetector(
              onTap:
                  (widget.goBackCallback ?? () {}) as GestureTapCallback, // 强转
              child: const Icon(
                Icons.chevron_left,
                color: Colors.black87,
              ),
            ),
          ),
        Expanded(
            child: Container(
          decoration: BoxDecoration(
              color: Colors.grey[200], borderRadius: BorderRadius.circular(17)),
          height: 34,
          margin: const EdgeInsets.only(right: 10),
          child: TextField(
              controller: _controller,
              focusNode: _focus, // 控制焦点
              style: const TextStyle(fontSize: 14),
              onTap: () {
                if (widget.onSearchSubmit == null) {
                  _focus.unfocus(); // 不是搜索页则失去焦点
                }
                if (widget.onSearch != null) {
                  widget.onSearch!();
                }
              },
              onChanged: (value) {
                setState(() => _searchWord = value);
              },
              onSubmitted: widget.onSearchSubmit,
              textInputAction: TextInputAction.search,
              decoration: InputDecoration(
                hintText: widget.defaultInputValue,
                hintStyle: const TextStyle(color: Colors.grey, fontSize: 14),
                border: InputBorder.none,
                contentPadding: const EdgeInsets.only(top: 1, left: -10), // 负数
                icon: const Padding(
                  padding: EdgeInsets.only(left: 10, top: 2),
                  child: Icon(
                    Icons.search,
                    color: Colors.grey,
                    size: 18,
                  ),
                ),
                suffixIcon: GestureDetector(
                    onTap: () {
                      _onClean();
                    },
                    child: Icon(
                      Icons.clear,
                      color:
                          _searchWord == '' ? Colors.transparent : Colors.grey,
                      size: 18,
                    )),
              )),
        )),
        if (widget.onCancel != null || false)
          Padding(
            padding: const EdgeInsets.only(right: 10),
            child: GestureDetector(
              onTap: (widget.onCancel ?? () {}) as GestureTapCallback,
              child: const Text(
                '取消',
                style: TextStyle(
                    fontSize: 14,
                    color: Colors.black,
                    fontWeight: FontWeight.w600),
              ),
            ),
          ),
        if (widget.showMap ?? false)
          const CommonImage(
            'static/icons/widget_search_bar_map.png',
            width: 40,
            height: 40,
          ),
      ]),
    );
  }
}
